/*
 * @Description: 光盘检测 - 可用性检测
 * @Author: Rfan
 * @Date: 2022-10-14 10:41:01
 * @LastEditTime: 2022-10-21 18:11:22
 */

import { Button, Checkbox, Space, Typography } from 'antd';
import cs from 'classnames';
import classnames from 'classnames/bind';
import { useAddPlanDialogContext } from '../../../contexts/AddPlanDialogContext';
import { CheckProps, EnumUsabilityRuleType } from '../../../../../../dictionaries';
import type { IPropsRule } from '../../../../../../interface';
import styles from '../../../styles/entryPanes.module.scss';
import mainStyles from '../../../styles/main.module.scss';
import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { isNullObj } from '@/utils';

const cx = classnames.bind(styles);
const mainCx = classnames.bind(mainStyles);
// CD可用性规则列表
const CDUsabilityList = [8, 9];

// CD可用性规则列表类型
type TRuleList = IPropsRule<EnumUsabilityRuleType>[];

// CD可用性完整类型
interface IUsabilityFullRules {
  ruleType: CheckProps.Usability;
  rules: TRuleList;
}

const CDUsabilityCheck = (props: any, ref: any) => {
  const {
    currentRuleInfo,
    getPropertyRules,
    selectAllRules,
    selectInverseRules,
    getRuleState,
    setRuleState
  } = useAddPlanDialogContext();

  // 规则列表
  const [ruleList, setRuleList] = useState<TRuleList>([]);

  useImperativeHandle(ref, () => ({
    save: () => {
      const fullRules: IUsabilityFullRules = {
        ruleType: CheckProps.Usability,
        rules: ruleList
      };
      return fullRules;
    }
  }));

  /**
   * @description: 获取到规则后回写
   */
  useEffect(() => {
    if (currentRuleInfo && !isNullObj(currentRuleInfo)) {
      const ruleObj = getPropertyRules(CheckProps.Usability);
      setRuleList(ruleObj?.rules || []);
    }
  }, [currentRuleInfo]);

  return (
    <>
      <div className={cs(mainCx('property-wrap'), cx('property-authenticity'))}>
        <Space className={mainCx('property--title-wrap')} align="center">
          <Space>
            <h6 className={mainCx('property--title')}>光盘格式包</h6>
            <Typography.Text type="secondary">检测原文格式需满足以下选择的格式范围</Typography.Text>
          </Space>
          <div className={mainCx('select-btn')}>
            <Button
              type="text"
              size="small"
              onClick={() => {
                setRuleList(selectAllRules(CDUsabilityList));
              }}
            >
              全选
            </Button>{' '}
            /{' '}
            <Button
              type="text"
              size="small"
              onClick={() => {
                setRuleList(selectInverseRules(CDUsabilityList, ruleList));
              }}
            >
              反选
            </Button>
          </div>
        </Space>

        <Space direction="vertical">
          <Checkbox
            checked={getRuleState(EnumUsabilityRuleType.DiskMetadataReadability, ruleList)}
            onChange={(evt) => {
              setRuleState(
                evt.target.checked,
                EnumUsabilityRuleType.DiskMetadataReadability,
                ruleList,
                setRuleList
              );
            }}
          >
            信息包中元数据的可读性检测（检测归档信息包中存放元数据的XML文件是否可以正常解析、读取数据）
          </Checkbox>
          <Checkbox
            checked={getRuleState(EnumUsabilityRuleType.DiskContentFormatCompliance, ruleList)}
            onChange={(evt) => {
              setRuleState(
                evt.target.checked,
                EnumUsabilityRuleType.DiskContentFormatCompliance,
                ruleList,
                setRuleList
              );
            }}
          >
            信息包中包含的内容数据格式合规性检测（对归档信息包是否包含非公开压缩算法、是否加密）
          </Checkbox>
        </Space>
      </div>
    </>
  );
};

export default forwardRef(CDUsabilityCheck);
